﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Text.RegularExpressions;

namespace BHA
{
    public partial class TrackEquip
    {
        const string SAPTITLE_REGEX = @"part number|sap number|engineer.*description";
        const string SAPTITLE_NAME = "TE_SapCol";

        private void TrackEquip_Startup(object sender, System.EventArgs e)
        {
            foreach (Excel.Range title_cell in Application.Intersect(TE_Title.RefersToRange, UsedRange))
            {
                if (string.IsNullOrWhiteSpace(title_cell.Text)) continue;
                if (Regex.IsMatch(title_cell.Text, SAPTITLE_REGEX, RegexOptions.IgnoreCase)) title_cell.Name = SAPTITLE_NAME;
            }
        }

        private void TrackEquip_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.TE_Title.Change += new Microsoft.Office.Interop.Excel.DocEvents_ChangeEventHandler(this.TE_Title_Change);
            this.Startup += new System.EventHandler(this.TrackEquip_Startup);
            this.Shutdown += new System.EventHandler(this.TrackEquip_Shutdown);

        }

        #endregion

        private void TE_Title_Change(Excel.Range Target)
        {
            if (Target.Count > 100) return; // Insert Column Event : No WellName Change
            Excel.Range intersect = Application.Intersect(Target, TE_Balance.RefersToRange); // Wellname Change
            if (intersect == null) return;
            foreach (Excel.Range wellnamecell in intersect)
            {   // Each Wellname Cell (changed)
                if (wellnamecell.Column > TE_Balance.Column & wellnamecell.Column < TE_Balance.Column + TE_Balance.Columns.Count - 1)
                {   // Neu b nam giua Current & Future Balance
                    List<ItemZone> zonelist = Globals.PerfPlan.RefreshAllItemSAP();
                    ItemSAPCollection wellitem = new ItemSAPCollection(); // Using Collection when add same NoSAP will Increase to existing NoSAP instead adding new duplicate Sap No.
                    foreach (ItemZone zoneitem in zonelist)
                    {
                        if (zoneitem.WellName == wellnamecell.Text.Trim()) wellitem.AddRange(zoneitem.Items);
                    }   // Got list of ItemSAP for that well
                    FillQuantity(wellnamecell, wellitem.Items); // Fill All ItemSAP start from TopCell = wellnamecell
                }
            }
        }

        private void FillQuantity(Excel.Range topcell, List<ItemSAP> list)
        {
            foreach (Excel.Range wellcell in Application.Intersect(topcell.EntireColumn, UsedRange))
            {   // Duyet qua mot luot cac o trong cot Wellname changed :
                Excel.Range sapcell = Cells[wellcell.Row, Range[SAPTITLE_NAME].Column];
                if (!ItemSAP.IsExtSAP(sapcell.Text)) continue; // Neu Current Row khong phai Row SAP Item thi Row Ke
                // Row la SAP Item thi kiem tra va dien Quantity
                foreach (ItemSAP itemsap in list)
                {   // Duyet qua mot luot cac ItemSAP trong list item cua WellName
                    if (sapcell.Text.Trim() == itemsap.NoSAP)
                    {
                        itemsap.FillQuantity(wellcell);
                    }
                }
            }
        }
    }
}
